package xcmg.device.web.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.service.account.InvoiceService;
import xcmg.device.service.vo.account.InvoiceDetailVO;
import xcmg.device.service.vo.account.InvoiceVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/invoice")
public class InvoiceController extends BasicController {

    @Autowired
    private InvoiceService invoiceService;


//    @RequestMapping(value = "/sapPost", method = RequestMethod.POST)
//    public ActionResult sapPost() {
//        ErrorCode errorCode;
//        try {
////            errorCode = sapInvoiceService.sapPost(getOrgId());
//        } catch (Exception e) {
//            LogHelper.monitor(getExceptionAllinformation(e));
//            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
//        }
//        return actionResult(errorCode);
//    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<InvoiceVO> condition)throws Exception{
        PageCommonVO pageCommonVO = invoiceService.list(condition, getOrgId());
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/detailList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult detailList(@RequestBody SearchCommonVO<InvoiceDetailVO> condition)throws Exception{
        InvoiceDetailVO invoiceDetailVO = condition.getFilters();
        //invoiceDetailVO.setOrgId(getOrgId());
        condition.setFilters(invoiceDetailVO);
        PageCommonVO pageCommonVO = invoiceService.detailList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/sign",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult sign(@RequestBody InvoiceVO invoiceVO)throws Exception{
        invoiceVO.setSignId(getUserId());
        invoiceVO.setSignName(getUserName());
        return actionResult(invoiceService.sign(invoiceVO));
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        invoiceService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(InvoiceVO condition)throws Exception {
        invoiceService.exportAll(condition, getOrgId());
    }

    private String getExceptionAllinformation(Exception ex){
        String sOut = "\r\n";
        sOut += ex.getMessage();
        StackTraceElement[] trace = ex.getStackTrace();
        for (StackTraceElement s : trace) {
            sOut += "\tat " + s + "\r\n";
        }
        return sOut;
    }

//    @Scheduled(cron = "0 20 5 * * ? ")
//    @SchedulerLock(name = "scheduledTaskName16", lockAtMostFor = 14*60*1000, lockAtLeastFor = 14*60*1000)
//    public void syncPurchasePlan() throws Exception {
//        LogHelper.monitor("执行读取发票信息定时器任务："+ DateUtil.formatDateTime(new Date()));
////        sapInvoiceService.sapPost("ALL");
//    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody InvoiceVO invoiceVO) {
        return invoiceService.update(invoiceVO);
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult itemById(String id) {
        return actionValueResult(invoiceService.getById(id));
    }
}
